#coding:utf-8
import os
from common.koufuApi import KoufuApi
from common.koufuShop20Api import KoufuShop20Api
from common.tool import Tools

__author__ = 'yangxianming'

from common.KoufuConf import KoufuConf
from common.picAddress import PicAddress
import json
import requests

'''
创建商品
批量创建商品
common下的picAddress.py 定义了100多个图片地址，虽然看上去都是一个图片
'''

def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.shop20_domain
    kou_fu_shop_api = KoufuShop20Api()
    script_name = os.path.basename(__file__)
    tool = Tools
    tf = True
    test_result = ""

    ##登陆
    head = {"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}
    p = kou_fu_shop_api.shop_login()
    r = requests.post(test_url + kou_fu_shop_api.shop_login(get_path=1) ,data=p, headers=head)
    reCookie = r.headers.get("set-cookie")
    jsessionid = reCookie[11:47]
    print(jsessionid)
    # kou_fu_conf.koufu_shop_headers["Cookie"] = "JSESSIONID="+jsessionid
    kou_fu_conf.koufu_shop_headers = {"Cookie": "JSESSIONID="+jsessionid,
                                      "Content-Type":"application/json"}


    for i in range(1):
        iz = str(i).zfill(4)
        # i = "yxmyxmyxm"
        goods_sn = "AT14" + iz
        name = "横行霸道14" + iz
        ##创建产品 PicAddress.pic[7]
        p = kou_fu_shop_api.get_goods_save(update_body={"primaryPicUrl": PicAddress.pic[i],"goodsSn":goods_sn,"name": name})
        r = requests.post(test_url+kou_fu_shop_api.get_goods_save(get_path=1), json.dumps(p), headers=kou_fu_conf.koufu_shop_headers)
        print(r.json())

        ##获取产品列表
        p = kou_fu_shop_api.get_goods_list()
        r = requests.get(test_url+kou_fu_shop_api.get_goods_list(get_path=1), p, headers=kou_fu_conf.koufu_shop_headers)
        # print("这个是接口api_goods_product_list返回的全部参数\n", r.json())
        goods_id = r.json()["page"]["list"][0]["id"]
        print(goods_id)

        ##配置产品sku 这里的goodsSn与创建商品的goodsSn是不同的
        goods_sku_sn = "ATSKU" + iz
        p = kou_fu_shop_api.get_product_save(update_body={"goodsSn":goods_sku_sn,"goodsId": goods_id})
        r = requests.post(test_url+kou_fu_shop_api.get_product_save(get_path=1), json.dumps(p), headers=kou_fu_conf.koufu_shop_headers)
        print(r.json())

        ##查询productId
        p = kou_fu_shop_api.get_product_list(update_body={"goodsId":goods_id})
        r = requests.get(test_url+kou_fu_shop_api.get_product_list(get_path=1), p, headers=kou_fu_conf.koufu_shop_headers)
        # print("这个是接口api_goods_product_list返回的全部参数\n", r.json())
        productId = r.json()["page"]["list"][0]["id"]
        print(productId)

        ##添加到标签中
        p = kou_fu_shop_api.get_attributecategoryrel_save(update_body={"productId":productId})
        r = requests.post(test_url+kou_fu_shop_api.get_attributecategoryrel_save(get_path=1), json.dumps(p), headers=kou_fu_conf.koufu_shop_headers)
        print(r.json())



    #定义预期描述
    print(str(tf))
    # print(test_result)
    expect_result = "返回参数ebuyCode是否是空"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

case()